﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace chartsRT.PlotChart
{
    internal  class Data
    {
        public string title { get; set; }
        public List<int> values { get; set; }
        public string color { get; set; }
    }

    internal class RootObject
    {
        public string handle { get; set; }
        public string key { get; set; }
        public Data data { get; set; }
    }
    public static class PlotChartUT
    {
       public static string getParams(string DS)
        {
            try
            {
                string pattern = @"""\d"":";
                string JSON = Regex.Replace(string.Format("[{0}]", DS.Substring(1, DS.Length - 2)).ToString(), pattern, string.Empty);
                List<RootObject> dataSource = JsonConvert.DeserializeObject<List<RootObject>>(JSON);
                dynamic Result = new System.Dynamic.ExpandoObject();
               var min= Result.min = dataSource.Min(ee => ee.data.values.Min());
               var max = Result.max = dataSource.Max(ee => ee.data.values.Max());
                Result.length = dataSource.Max(ee => ee.data.values.Max()).ToString().Length * 14;
                Result.step = (max- min) / dataSource.Min(ee => ee.data.values.Count());
                return JsonConvert.SerializeObject(Result);
            }
            catch (Exception ex) {
                var x = ex;
                return null;
            }
            
        }
    }
}
